#!/usr/bin/python
#coding=utf-8
import sys
import os
import logging
logging.basicConfig(level=logging.DEBUG,
##									format='%(thread)d %(asctime)s %(funcName)s %(message)s',
##									format='%(asctime)s %(name)s %(levelname)s %(funcName)s %(message)s',
									format='%(funcName)s %(message)s',
                  datefmt= '%H:%M:%S')

def initDjangoORM():
	u'''初始化Django的ORM模块'''
	os.environ['DJANGO_SETTINGS_MODULE']='postgetter.settings'
	sys.path.append(os.path.dirname(os.path.abspath(__file__)))
	# 重要 为了能使用django的orm模块，需要创建目录btView，在里面放上空
	#  文件__init__.py和文件models.py,这样django就会去数据库中找btView_Video,
	#  btView_Screenshot，btView是创建的另一个ajango应用的app_name
	# 如何单独使用ajango的orm模块见 http://wiki.woodpecker.org.cn/moin/UsingDjangoAsAnStandaloneORM
	from postgetter.getpost.models import Forum,Sector,Post,Reply
	globals()['Forum']=Forum
	globals()['Sector']=Sector
	globals()['Post']=Post
	globals()['Reply']=Reply

	from django.db import transaction
	globals()['transaction']=transaction



def initData():
	# qbq
	try:
		qbq=Forum.objects.filter(name='qbq')[0:1].get()
	except Forum.DoesNotExist:
		qbq=Forum(name='qbq',home_url='http://www.small-arms.org/',login_url='http://www.small-arms.org/bbs/login.aspx',
			usr='',pwd='',disabled=False)
		qbq.save()

	try:
		qbq1=Sector.objects.filter(name='qbq-茶馆')[0:1].get()
	except Sector.DoesNotExist:
		qbq1=Sector(forum=qbq,name='qbq-茶馆',locate_id='12',url='http://www.small-arms.org/showforum.aspx?forumid=12&page=%d',
			base_url='http://www.small-arms.org/bbs/',
			url_pattern='/html/body/form[@name="batch"]/table[@class="mainbox tableborder"]/tr[@*]',
			posts_xpath='', post_title_xpath='', post_author_xpath='', post_reply_xpath='')
		qbq1.save()

	try:
		qbq2=Sector.objects.filter(name='qbq-警务')[0:1].get()
	except Sector.DoesNotExist:
		qbq2=Sector(forum=qbq,name='qbq-警务',locate_id='9',url='http://www.small-arms.org/showforum.aspx?forumid=9&page=%d',

			base_url='http://www.small-arms.org/bbs/',
			url_pattern='/html/body/form[@name="batch"]/table[@class="mainbox tableborder"]/tr[@*]',
			posts_xpath='', post_title_xpath='', post_author_xpath='', post_reply_xpath='')
		qbq2.save()


	# cd
	try:
		cd=Forum.objects.filter(name='cd')[0:1].get()
	except Forum.DoesNotExist:
		cd=Forum(name='cd',home_url='http://lt.cjdby.net/index.php',login_url='http://lt.cjdby.net/logging.php?action=login&loginsubmit=yes',
			usr='',pwd='',disabled=False)
		cd.save()

	try:
		cd1=Sector.objects.filter(name='cd-生活茶馆')[0:1].get()
	except Sector.DoesNotExist:
		cd1=Sector(forum=cd,name='cd-生活茶馆',locate_id='13',url='http://lt.cjdby.net/forum-13-%d.html',
			base_url='http://lt.cjdby.net/',
			url_pattern='//form[@id="moderate"]/table/tbody[starts-with(@id,"normalthread_")=true()]',
			posts_xpath='', post_title_xpath='', post_author_xpath='', post_reply_xpath='')
		cd1.save()

	try:
		cd2=Sector.objects.filter(name='cd-中外历史(原创讨论)')[0:1].get()
	except Sector.DoesNotExist:
		cd2=Sector(forum=cd,name='cd-中外历史',locate_id='86',url='http://lt.cjdby.net/forum-86-%d.html',
			base_url='http://lt.cjdby.net/',
			url_pattern='//form[@id="moderate"]/table/tbody[starts-with(@id,"normalthread_")=true()]',
			posts_xpath='', post_title_xpath='', post_author_xpath='', post_reply_xpath='')
		cd2.save()

	# sbz
	sbz=None
	try:
		sbz=Forum.objects.filter(name='sbz')[0:1].get()
	except Forum.DoesNotExist:
		cd=Forum(name='sbz',home_url='http://www.sbanzu.com/board_list.asp',login_url='http://www.sbanzu.com/checkuser.asp',
			usr='',pwd='',disabled=False)
		cd.save()

	try:
		cd1=Sector.objects.filter(name='sbz-自然科学')[0:1].get()
	except Sector.DoesNotExist:
		cd1=Sector(forum=sbz,name='sbz-自然科学',locate_id='22',url='http://www.sbanzu.com/boarddisplay.asp?BoardID=22&Page=%d',
			base_url='http://www.sbanzu.com/',
			url_pattern='//table[@class="maintable"][1]/tr[position()>1]',
			posts_xpath='', post_title_xpath='', post_author_xpath='', post_reply_xpath='')
		cd1.save()


	try:
		cd2=Sector.objects.filter(name='sbz-古代战争')[0:1].get()
	except Sector.DoesNotExist:
		cd1=Sector(forum=sbz,name='sbz-古代战争',locate_id='16',url='http://www.sbanzu.com/boarddisplay.asp?BoardID=16&Page=%d',
			base_url='http://www.sbanzu.com/',
			url_pattern='//table[@class="maintable"][1]/tr[position()>1]',
			posts_xpath='', post_title_xpath='', post_author_xpath='', post_reply_xpath='')
		cd1.save()


if __name__ == '__main__':
	reload(sys)
	sys.setdefaultencoding('utf-8')

	initDjangoORM() # django will set env['TZ'] according to the settings.py of django project
	logging.info('Time Zone: %s',os.environ['TZ'])
##	os.environ['TZ']='Asia/Shanghai'
	initData()
	logging.info('done.')
